package jsu.xnm.evelyn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jsu.xnm.evelyn.bean.Category;
import jsu.xnm.evelyn.bean.Dish;
import jsu.xnm.evelyn.bean.Setmeal;
import jsu.xnm.evelyn.exception.CustomException;
import jsu.xnm.evelyn.mapper.CategoryMapper;
import jsu.xnm.evelyn.service.CategoryService;
import jsu.xnm.evelyn.service.DishService;
import jsu.xnm.evelyn.service.SetmealService;
import org.springframework.stereotype.Service;

/**
 * @author xnm
 * @version 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private DishService dishService;

    @Resource
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //查询当前的菜品是否关联相应的【菜品】
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = Wrappers.lambdaQuery();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        long count1 = dishService.count(dishLambdaQueryWrapper);

        if(count1 > 0){
            //有关联的菜品不可删除
            throw new CustomException("当前分类下关联了菜品无法删除!");
        }
        //查询当前的菜品是否关联相应的【套餐】
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = Wrappers.lambdaQuery();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        long count2 = setmealService.count(setmealLambdaQueryWrapper);

        if(count2 > 0){
            //有关联的套餐不可删除
            throw new CustomException("当前分类下关联了套餐无法删除!");
        }
        super.removeById(id);
    }
}
